package com.intelligentmeterreadingsys.settingsys.activity;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.WaterInfoVO;
import com.intelligentmeterreadingsys.waterlist.copywater.db.CopyWaterDao;

import jxl.CellView;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

/**
 * 导出Excel数据
 * @author king
 *
 */
public class OperateExcelActivity extends Activity{
	String path = "mnt/sdcard/test.xls";
	public Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settingsys);
	     writeExcel(path);
		
	}
	
	 public void writeExcel(String fileName){
		 WritableWorkbook wbook=null;
		 try {
			 wbook = Workbook.createWorkbook(new File(fileName));
			 if(wbook!=null){
				 System.out.println("------------->>>>>>>");
				 WritableSheet wsheet = wbook.createSheet("抄表信息", 0); //工作表名称  
				//设置Excel字体  
				 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,  
				 WritableFont.BOLD, false,  
				 jxl.format.UnderlineStyle.NO_UNDERLINE,  
				 jxl.format.Colour.BLACK);  
				 CellView cv = new CellView();
				 cv.setAutosize(true);
				
				 WritableCellFormat titleFormat = new WritableCellFormat(wfont);  
				 String[] title = { "水表编号", "抄表止度", "用水量", "抄表日期" };  
				//设置Excel表头  
				 for (int i = 0; i < title.length; i++) {  
				 Label excelTitle = new Label(i, 0, title[i], titleFormat);  
				 wsheet.addCell(excelTitle);  
				 wsheet.setColumnView(i, cv);
				 }  
				 CopyWaterDao dao=new CopyWaterDao(this);
				
				 List<WaterInfoVO> list= dao.queryAllNoUpload();
				 dao.close();
				 for(int i=0;i<list.size();i++){
					 System.out.println("-------------->");
					 WaterInfoVO water=list.get(i);
					 Label content1 = new Label(0, i+1,water.getWaternumber() );  
					 Label content2 = new Label(1, i+1, water.getThismonthwater()+"");  
					 Label content3 = new Label(2, i+1, water.getThismonthwater()-water.getLastmonthwater()+"");  
					 Label content4 = new Label(3, i+1,water.getThisdate());  
					 wsheet.addCell(content1);  
					 wsheet.addCell(content2);  
					 wsheet.addCell(content3);  
					 wsheet.addCell(content4);  
					 wsheet.setColumnView(i, cv);
				 }
				 wbook.write(); //写入文件  
				 wbook.close();  
				 System.out.println("导出成功");
				 
			 }
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (RowsExceededException e) {
			
			e.printStackTrace();
		} catch (WriteException e) {
			
			e.printStackTrace();
		} 
		 
     }
 


}
